﻿#include "axisthresholdwidget.h"

QAxisThresholdWidget::QAxisThresholdWidget(int axisNo,QWidget *parent) : QWidget(parent)
{
    this->setMinimumHeight(600);
    _i32AixsNo = axisNo;

    InitParamConfig();

    this->setStyleSheet("background-color:transparent;");

    InitUI();
    connect(_pParamSaveBtn,&QPushButton::clicked,this,&QAxisThresholdWidget::onParamSaveButtonClick);

//    void (QComboBox::*fn)(int) = &QComboBox::currentIndexChanged;
//    connect(_pParamModifyModeComBox,fn,this,[=](int nIndex){
//        bool bEnable = nIndex == 2?false:true;
//        for(int i = 0; i < _pParamSetWidget->rowCount()-1;i++)
//        {
//            _pParamSetWidget->cellWidget(i,3)->setEnabled(bEnable);
//        }
//    });
}

/**
* @brief 初始化设置的参数
* @note
*
*/
void QAxisThresholdWidget::InitParamConfig()
{
    st_cmd_attr l_cmd_attr;
    l_cmd_attr.sName = "速度门限设置";
    l_cmd_attr.i32Cmdid = 0x5A;
    l_cmd_attr.lfCoeff = 10.0;
    l_cmd_attr.sUnit = "°/s";
    l_cmd_attr.i32Valuelen = 1;
    l_cmd_attr.lfMaxValue = 10.0;
    l_cmd_attr.lfMinValue = 0.0;
    if(_i32AixsNo == M_AXIS_AZI)
    {
        l_cmd_attr.ptrValue = (uchar*)&_rcvdata_11.azimuth_speed_limit;
    }
    if(_i32AixsNo == M_AXIS_PICTH)
    {
        l_cmd_attr.ptrValue = (uchar*)&_rcvdata_11.pitch_speed_limit;
    }
    _cmdMap.insert(0,l_cmd_attr);

    l_cmd_attr.sName = "电流门限设置";
    l_cmd_attr.i32Cmdid = 0x5B;
    l_cmd_attr.lfCoeff = 10.0;
    l_cmd_attr.sUnit = "A";
    l_cmd_attr.i32Valuelen = 1;
    l_cmd_attr.lfMaxValue = 3.0;
    l_cmd_attr.lfMinValue = 0.0;
    if(_i32AixsNo == M_AXIS_AZI)
    {
        l_cmd_attr.ptrValue = (uchar*)&_rcvdata_11.azimuth_current_limit;
    }
    if(_i32AixsNo == M_AXIS_PICTH)
    {
        l_cmd_attr.ptrValue = (uchar*)&_rcvdata_11.pitch_current_limit;
    }
    _cmdMap.insert(1,l_cmd_attr);


    l_cmd_attr.sName = "过流门限设置";
    l_cmd_attr.i32Cmdid = 0x5C;
    l_cmd_attr.lfCoeff = 10.0;
    l_cmd_attr.sUnit = "A";
    l_cmd_attr.i32Valuelen = 1;
    l_cmd_attr.lfMaxValue = 3.0;
    l_cmd_attr.lfMinValue = 0.0;
    if(_i32AixsNo == M_AXIS_AZI)
    {
        l_cmd_attr.ptrValue = (uchar*)&_rcvdata_11.azimuth_overcurrent_limit;
    }
    if(_i32AixsNo == M_AXIS_PICTH)
    {
        l_cmd_attr.ptrValue = (uchar*)&_rcvdata_11.pitch_overcurrent_limit;
    }
    _cmdMap.insert(2,l_cmd_attr);

    l_cmd_attr.sName = "加速度门限设置";
    l_cmd_attr.i32Cmdid = 0x5D;
    l_cmd_attr.lfCoeff = 10.0;
    l_cmd_attr.sUnit = "°/s²";
    l_cmd_attr.i32Valuelen = 1;
    l_cmd_attr.lfMaxValue = 10.0;
    l_cmd_attr.lfMinValue = 0.0;
    if(_i32AixsNo == M_AXIS_AZI)
    {
        l_cmd_attr.ptrValue = (uchar*)&_rcvdata_11.azimuth_acc_limit;
    }
    if(_i32AixsNo == M_AXIS_PICTH)
    {
        l_cmd_attr.ptrValue = (uchar*)&_rcvdata_11.pitch_acc_limit;
    }
    _cmdMap.insert(3,l_cmd_attr);

    l_cmd_attr.sName = "角位置偏置设置";
    l_cmd_attr.i32Cmdid = 0xD1;
    l_cmd_attr.lfCoeff = 16777216/360.0;
    l_cmd_attr.sUnit = "°";
    l_cmd_attr.i32Valuelen = 3;
    l_cmd_attr.lfMaxValue = 360.0;
    l_cmd_attr.lfMinValue = 0.0;
    if(_i32AixsNo == M_AXIS_AZI)
    {
        l_cmd_attr.ptrValue = (uchar*)&_rcvdata_55.azimuth_pos_offset;
    }
    if(_i32AixsNo == M_AXIS_PICTH)
    {
        l_cmd_attr.ptrValue = (uchar*)&_rcvdata_55.pitch_pos_offset;
    }
    _cmdMap.insert(4,l_cmd_attr);

    l_cmd_attr.sName = "零位偏置设置";
    l_cmd_attr.i32Cmdid = 0xD2;
    l_cmd_attr.lfCoeff = 16777216/360.0;
    l_cmd_attr.sUnit = "°";
    l_cmd_attr.i32Valuelen = 3;
    l_cmd_attr.lfMaxValue = 360.0;
    l_cmd_attr.lfMinValue = 0.0;
    if(_i32AixsNo == M_AXIS_AZI)
    {
        l_cmd_attr.ptrValue = (uchar*)&_rcvdata_55.azimuth_zero_offset;
    }
    if(_i32AixsNo == M_AXIS_PICTH)
    {
        l_cmd_attr.ptrValue = (uchar*)&_rcvdata_55.pitch_zero_offset;
    }
    _cmdMap.insert(5,l_cmd_attr);

//    l_cmd_attr.sName = "滤波系数设置";
//    l_cmd_attr.lfCoeff = 16777216/360.0;
//    l_cmd_attr.sUnit = "°";
//    l_cmd_attr.i32Valuelen = 3;
//    _cmdMap.insert(0xD3,"滤波系数设置");

    l_cmd_attr.sName = "超速保护门限";
    l_cmd_attr.i32Cmdid = 0xD6;
    l_cmd_attr.lfCoeff = 10.0;
    l_cmd_attr.sUnit = "°";
    l_cmd_attr.i32Valuelen = 1;
    l_cmd_attr.lfMaxValue = 200.0;
    l_cmd_attr.lfMinValue = 0.0;
    if(_i32AixsNo == M_AXIS_AZI)
    {
        l_cmd_attr.ptrValue = (uchar*)&_rcvdata_11.azimuth_overspeed_limit;
    }
    if(_i32AixsNo == M_AXIS_PICTH)
    {
        l_cmd_attr.ptrValue = (uchar*)&_rcvdata_11.pitch_overspeed_limit;
    }
    _cmdMap.insert(6,l_cmd_attr);


    l_cmd_attr.sName = "位置滤波系数";
    l_cmd_attr.i32Cmdid = 0xD3;
    l_cmd_attr.lfCoeff = 1;
    l_cmd_attr.sUnit = "°";
    l_cmd_attr.i32Valuelen = 1;
    l_cmd_attr.lfMaxValue = 255.0;
    l_cmd_attr.lfMinValue = 0.0;
    if(_i32AixsNo == M_AXIS_AZI)
    {
        l_cmd_attr.ptrValue = (uchar*)&_rcvdata_55.azimuth_filter_param[0];
    }
    if(_i32AixsNo == M_AXIS_PICTH)
    {
        l_cmd_attr.ptrValue = (uchar*)&_rcvdata_55.pitch_filter_param[0];
    }
    _cmdMap.insert(7,l_cmd_attr);


    l_cmd_attr.sName = "转速滤波系数";
    l_cmd_attr.i32Cmdid = 0xD3;
    l_cmd_attr.lfCoeff = 1;
    l_cmd_attr.sUnit = "°";
    l_cmd_attr.i32Valuelen = 1;
    l_cmd_attr.lfMaxValue = 255.0;
    l_cmd_attr.lfMinValue = 0.0;
    if(_i32AixsNo == M_AXIS_AZI)
    {
        l_cmd_attr.ptrValue = (uchar*)&_rcvdata_55.azimuth_filter_param[1];
    }
    if(_i32AixsNo == M_AXIS_PICTH)
    {
        l_cmd_attr.ptrValue = (uchar*)&_rcvdata_55.pitch_filter_param[1];
    }
    _cmdMap.insert(8,l_cmd_attr);

    l_cmd_attr.sName = "电流滤波系数";
    l_cmd_attr.i32Cmdid = 0xD3;
    l_cmd_attr.lfCoeff = 1;
    l_cmd_attr.sUnit = "°";
    l_cmd_attr.i32Valuelen = 1;
    l_cmd_attr.lfMaxValue = 255.0;
    l_cmd_attr.lfMinValue = 0.0;
    if(_i32AixsNo == M_AXIS_AZI)
    {
        l_cmd_attr.ptrValue = (uchar*)&_rcvdata_55.azimuth_filter_param[2];
    }
    if(_i32AixsNo == M_AXIS_PICTH)
    {
        l_cmd_attr.ptrValue = (uchar*)&_rcvdata_55.pitch_filter_param[2];
    }
    _cmdMap.insert(9,l_cmd_attr);

    if(_i32AixsNo == M_AXIS_AZI)
    {
        l_cmd_attr.sName = "机械角度左限位";
    }
    if(_i32AixsNo == M_AXIS_PICTH)
    {
        l_cmd_attr.sName = "机械角度上限位";
    }
    l_cmd_attr.i32Cmdid = 0xD8;
    l_cmd_attr.lfCoeff = 10.0;
    l_cmd_attr.sUnit = "°/s";
    l_cmd_attr.i32Valuelen = 1;
    l_cmd_attr.lfMaxValue = 360;
    l_cmd_attr.lfMinValue = 0;
    if(_i32AixsNo == M_AXIS_AZI)
    {
        l_cmd_attr.ptrValue = (uchar*)&_rcvdata_55.azimuth_pos_max_limit;
    }
    if(_i32AixsNo == M_AXIS_PICTH)
    {
        l_cmd_attr.ptrValue = (uchar*)&_rcvdata_55.pitch_pos_max_limit;
    }
    _cmdMap.insert(10,l_cmd_attr);


    if(_i32AixsNo == M_AXIS_AZI)
    {
        l_cmd_attr.sName = "机械角度右限位";
    }
    if(_i32AixsNo == M_AXIS_PICTH)
    {
        l_cmd_attr.sName = "机械角度下限位";
    }
    l_cmd_attr.i32Cmdid = 0xD8;
    l_cmd_attr.lfCoeff = 10.0;
    l_cmd_attr.sUnit = "°/s";
    l_cmd_attr.i32Valuelen = 1;
    l_cmd_attr.lfMaxValue = 360;
    l_cmd_attr.lfMinValue = 0;

    if(_i32AixsNo == M_AXIS_AZI)
    {
        l_cmd_attr.ptrValue = (uchar*)&_rcvdata_55.azimuth_pos_min_limit;
    }
    if(_i32AixsNo == M_AXIS_PICTH)
    {
        l_cmd_attr.ptrValue = (uchar*)&_rcvdata_55.pitch_pos_min_limit;
    }
    _cmdMap.insert(11,l_cmd_attr);
}
/**
* @brief
* @note
*
*/
 void QAxisThresholdWidget::InitUI()
 {
     QGridLayout* playout = new QGridLayout(this);
     playout->addWidget(createParamSetWidget(),0,0,1,1);
 }


 /**
* @brief
* @note
*
*/
QWidget* QAxisThresholdWidget::createParamSetWidget()
{
    _pParamSaveBtn = new QPushButton("参数保存",this);
    QUIQss::setBtnQss(_pParamSaveBtn,"#1ABC9C", "#E6F8F5", "#2EE1C1", "#FFFFFF", "#16A086", "#A7EEE6");

    //参数设置
    _pParamSetWidget = new QTableWidget(this);
    QString sTableStyle = "QTableWidget{border:0px solid;border-top:1px solid rgb(150, 150, 150);";
    sTableStyle = sTableStyle + "font-size:16px;font-family: 'Microsoft YaHei';color:rgb(255,255,255);}";
    sTableStyle = sTableStyle + "QTableWidget::Item{border:0px solid rgb(150, 150, 150);border-bottom:1px solid rgb(150, 150, 150);}";
    _pParamSetWidget->setStyleSheet(sTableStyle);
    _pParamSetWidget->setShowGrid(false);

//    _pParamSetWidget->setStyleSheet("border:0px;font-size:16px;font-family: 'Microsoft YaHei';");
    _pParamSetWidget->horizontalHeader()->setStretchLastSection(true);//最后一列充满
    _pParamSetWidget->verticalHeader()->hide();
    _pParamSetWidget->setColumnCount(4);
    _pParamSetWidget->setRowCount(_cmdMap.size());

    QStringList header;
    header<<"参数名称"<<"设置值"<<"反馈值"<<"操作";
    _pParamSetWidget->setHorizontalHeaderLabels(header);
    _pParamSetWidget->setColumnWidth(1,100);
    _pParamSetWidget->setColumnWidth(2,100);


    for(int i = 0; i < _cmdMap.keys().size();i++)
    {
        int  l_key = _cmdMap.keys()[i];
        QString sName = _cmdMap[l_key].sName;

        _pParamSetWidget->setItem(i,0,new QTableWidgetItem(sName));
        _pParamSetWidget->item(i,0)->setFlags(Qt::ItemIsEnabled);
        _pParamSetWidget->setItem(i,1,new QTableWidgetItem("0.0"));
        _pParamSetWidget->item(i,1)->setToolTip(QString("数据范围:[%1,%2]").arg(_cmdMap[l_key].lfMinValue).arg(_cmdMap[l_key].lfMaxValue));
        _pParamSetWidget->setItem(i,2,new QTableWidgetItem("0.0"));
        _pParamSetWidget->item(i,2)->setFlags(Qt::ItemIsEnabled);

        _pParamSetWidget->item(i,1)->setTextAlignment(Qt::AlignCenter);
        _pParamSetWidget->item(i,2)->setTextAlignment(Qt::AlignCenter);

        QPushButton* pbutton = new QPushButton("设置",this);
        pbutton->setStyleSheet(
                    "QPushButton{padding:5px;border-radius:5px;color:rgb(255,255,255);font-size:18px;font-family: 'Microsoft YaHei';background-color: rgb(46,97,186);}"
                    "QPushButton:hover{padding:5px;border-radius:5px;color:white;font-size:18px;font-family: 'Microsoft YaHei';background-color: rgb(96,131,203);}"
                    "QPushButton:pressed{padding:5px;border-radius:5px;color:white;font-size:18px;font-family: 'Microsoft YaHei';background-color: rgb(139,165,217);}"
                    "QPushButton:disabled { background-color: rgb(200,200,200);color:black;}"
                                    );
        connect(pbutton,&QPushButton::clicked,this,&QAxisThresholdWidget::onSetParamButtonClick);
        _pParamSetWidget->setCellWidget(i,3,pbutton);
    }


    _pParamSetWidget->setSpan(_cmdMap.size()-5,3,3,1);

    _pParamSetWidget->setSpan(_cmdMap.size()-2,3,2,1);

    QWidget* pwidget = new QWidget(this);
    QGridLayout* playout = new QGridLayout(pwidget);

    playout->addWidget(_pParamSaveBtn,0,2,1,1);
    playout->addWidget(_pParamSetWidget,1,0,1,3);

    playout->setRowStretch(0,1);
    playout->setRowStretch(1,5);

    return pwidget;
}
/**
* @brief
* @note
*
*/
void QAxisThresholdWidget::onSetParamButtonClick()
{
    QPushButton *pushButton = dynamic_cast<QPushButton*>(this->sender());
    if(pushButton == nullptr)
    {
        return;
    }
    int x = pushButton->frameGeometry().x();
    int y = pushButton->frameGeometry().y();
    QModelIndex index = _pParamSetWidget->indexAt(QPoint(x, y));
    int row = index.row();

    if(row >=  _cmdMap.keys().size())
    {
        return;
    }

    //获取当前命令的属性值
    int l_key =  _cmdMap.keys()[row];
    st_cmd_attr l_cmd_attr = _cmdMap[l_key];
    int l_cmd_id = l_cmd_attr.i32Cmdid;
    float lfValue = _pParamSetWidget->item(row,1)->text().toFloat();
    if(lfValue > l_cmd_attr.lfMaxValue || lfValue < l_cmd_attr.lfMinValue)
    {
        QMessageBox *msgBox = new QMessageBox();
        msgBox->setWindowTitle("提示");
        msgBox->setText(QString("输入数据有误，有效范围为[%1,%2]").arg(l_cmd_attr.lfMinValue).arg(l_cmd_attr.lfMaxValue));
        msgBox->addButton("确定", QMessageBox::AcceptRole);
        msgBox->setStyleSheet("font-size:18px;font-family: 'Microsoft YaHei';background-color:white");
        msgBox->exec();
        delete msgBox;
        return;
    }


    fourchar l_currentvalue;
    l_currentvalue.i32data = lfValue*l_cmd_attr.lfCoeff;



    if(row == _cmdMap.keys().size()-5)//系数
    {
        l_currentvalue.data[2] = _pParamSetWidget->item(row,1)->text().toFloat()*l_cmd_attr.lfCoeff;
        l_currentvalue.data[1] = _pParamSetWidget->item(row+1,1)->text().toFloat()*l_cmd_attr.lfCoeff;
        l_currentvalue.data[0] = _pParamSetWidget->item(row+2,1)->text().toFloat()*l_cmd_attr.lfCoeff;
        l_currentvalue.data[3] = 0;
    }

    if(row == _cmdMap.keys().size()-2)//机械限位
    {
        //左限位
        twochar l_data;
        l_data.ui16data = _pParamSetWidget->item(row,1)->text().toFloat()*l_cmd_attr.lfCoeff;

        l_currentvalue.data[2] = l_data.data[0];
        l_currentvalue.data[3] = l_data.data[1];

        //右限位
        l_data.ui16data = _pParamSetWidget->item(row+1,1)->text().toFloat()*l_cmd_attr.lfCoeff;
        l_currentvalue.data[0] = l_data.data[0];
        l_currentvalue.data[1] = l_data.data[1];
    }

    emit signal_setparam(_i32AixsNo,l_cmd_id,l_currentvalue.i32data,l_cmd_attr.i32Valuelen);
}


/**
* @brief
* @note
*
*/
void QAxisThresholdWidget::showRcvdata11(_loopdata_11 rcvdata11)
{
    _rcvdata_11 = rcvdata11;

    for(int i = 0; i < _cmdMap.keys().size();i++)
    {
        int  l_key = _cmdMap.keys()[i];
        st_cmd_attr l_cmd_attr = _cmdMap[l_key];
        float lfdata =  cacldata(l_cmd_attr);
        _pParamSetWidget->item(i,2)->setText(QString::number(lfdata,'f',4));
    }

}

/**
* @brief
* @note
*
*/
void QAxisThresholdWidget::showRcvdata55(_loopdata_55 rcvdata55)
{
    _rcvdata_55 = rcvdata55;

    for(int i = 0; i < _cmdMap.keys().size();i++)
    {
        int  l_key = _cmdMap.keys()[i];
        st_cmd_attr l_cmd_attr = _cmdMap[l_key];
        float lfdata =  cacldata(l_cmd_attr);
        _pParamSetWidget->item(i,2)->setText(QString::number(lfdata,'f',4));
    }
}

/**
* @brief
* @note
*
*/
float QAxisThresholdWidget::cacldata(st_cmd_attr cmd_attr)
{
    if(cmd_attr.ptrValue == nullptr)
    {
        return 0.0;
    }


    float lfdata = 0.0;
    float lfcoeff = 1.0/cmd_attr.lfCoeff;

    //机械上下限  收发比例因子不一样  特殊处理
    if(cmd_attr.i32Cmdid == 0xD8)
    {
        lfcoeff = 1.0;
    }

    if(cmd_attr.i32Valuelen == 0x01)
    {
        lfdata = (unsigned int)(*cmd_attr.ptrValue) * lfcoeff;
    }

    if(cmd_attr.i32Valuelen == 0x02)
    {
        if(cmd_attr.bsigned == true)
        {
            lfdata = (short)(*cmd_attr.ptrValue) * lfcoeff;
        }
        else
        {
            lfdata = (unsigned short)(*cmd_attr.ptrValue) * lfcoeff;
        }
    }

    if(cmd_attr.i32Valuelen == 0x03)
    {
        fourchar l_data;
        l_data.data[0] = cmd_attr.ptrValue[2];
        l_data.data[1] = cmd_attr.ptrValue[1];
        l_data.data[2] = cmd_attr.ptrValue[0];
        l_data.data[3] = 0;
        lfdata = l_data.ui32data * lfcoeff;
    }

    if(cmd_attr.i32Valuelen == 0x04)
    {
        fourchar l_data;
        l_data.data[0] = cmd_attr.ptrValue[3];
        l_data.data[1] = cmd_attr.ptrValue[2];
        l_data.data[2] = cmd_attr.ptrValue[1];
        l_data.data[3] = cmd_attr.ptrValue[0];
        if(cmd_attr.bsigned == true)
        {
            lfdata = l_data.i32data * lfcoeff;
        }
        else
        {
            lfdata = l_data.ui32data * lfcoeff;
        }
    }

    return lfdata;
}


void QAxisThresholdWidget::setButtonStyleSheet(QPushButton* pBtn)
{

    pBtn->setStyleSheet(
                "QPushButton:checked { background-color: rgb(245,90,70);;color:white;}"
                "QPushButton:unchecked { background-color: rgb(255,255,255);;color:black;}"
                "QPushButton{background-color: rgb(255,255,255);padding:5px;border-radius:5px;color:black;"
                "font-size:18px;font-family: 'Microsoft YaHei';}"
                "QPushButton:disabled { background-color: rgb(200,200,200);color:black;}"

                                );


//    pBtn->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    pBtn->setMinimumHeight(20);
}

/**
* @brief
*
**/
void QAxisThresholdWidget::onParamSaveButtonClick()
{
    emit signal_setparam(_i32AixsNo,0xD7,0x0,0x01);
}
